package com.atguigu.srb.core.controller.admin;


import com.atguigu.guigu.common.result.R;
import com.atguigu.srb.core.pojo.entity.Borrower;
import com.atguigu.srb.core.pojo.entity.vo.BorrowerApprovalVO;
import com.atguigu.srb.core.pojo.entity.vo.BorrowerDetailVO;
import com.atguigu.srb.core.service.BorrowerService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 借款人 前端控制器
 * </p>
 *
 * @author Atguigu
 * @since 2022-11-19
 */
@RestController
@RequestMapping("/admin/core/borrower")
public class AdminBorrowerController {
    @Resource
    BorrowerService borrowerService;
    @ApiOperation("借款人审批")
    @PostMapping("approval")
    public R approval(@RequestBody BorrowerApprovalVO vo){
        borrowerService.approval(vo);
        return R.ok().message("审批完成");
    }
    @ApiOperation("查询借款人详情")
    @GetMapping("getBorrowerDetail/{id}")
    public R getBorrowerDetail(@PathVariable("id")String id){
       BorrowerDetailVO vo = borrowerService.getBorrowerDetail(id);
       return R.ok().data("item",vo);
    }

    @ApiOperation("带条件分页查询借款人列表")
    @GetMapping("list/{pageNum}/{pageSize}")
    public R list(@PathVariable("pageNum")Integer pageNum,
                  @PathVariable("pageSize")Integer pageSize,
                  String keyword){
        IPage<Borrower> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Borrower> wrapper = Wrappers.lambdaQuery(Borrower.class);
        if(StringUtils.isNotEmpty(keyword)){
            wrapper.like(Borrower::getName,keyword)
                    .or()
                    .like(Borrower::getMobile , keyword)
                    .or()
                    .like(Borrower::getIdCard,keyword);
        }
        borrowerService.page(page,wrapper);
        return R.ok().data("page",page);
    }
}

